package com.winai.launcher.manager;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Handler;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.widget.TextView;

import com.ks.projectbasictools.ActivityCollector;
import com.ks.projectbasictools.AppManager;
import com.ks.projectbasictools.base.SimpleView;
import com.ks.projectbasictools.utils.LogUtils;
import com.winai.launcher.R;
import com.winai.launcher.base.db.DB_Common;
import com.winai.launcher.utils.BaseUtils;
import com.winai.launcher.utils.StatusBarUtil;
import com.winai.launcher.view.guide.ConfigWifiActivity;
import com.winai.launcher.view.guide.GuideCompleteActivity;
import com.winai.launcher.view.guide.GuideVideoActivity;
import com.winai.launcher.view.set.SetWifiActionActivity;
import com.winai.launcher.view.set.SetWifiActivity;
import com.winai.launcher.view.set.SetWifiDetailActivity;
import com.winai.launcher.view.set.SetWifiLinkActivity;
import com.yhao.floatwindow.FloatWindow;
import com.yhao.floatwindow.MoveType;
import com.yhao.floatwindow.Screen;

/**
 * 作者：康少
 * 时间：2020/5/27 0027
 * 说明：无网络Tip
 */
public class WindowTipManager {
    private Context mContext;
    private static WindowTipManager mInstance;
    public static final String WINDOW_TAG = "no_network_float_window_tag";

    private AlphaAnimation showAnim, hideAnim;
    private SimpleView simpleView;

    public static synchronized WindowTipManager getInstance(Context mContext) {
        if (mInstance == null) {
            synchronized (WindowTipManager.class) {
                if (null == mInstance) {
                    mInstance = new WindowTipManager(mContext.getApplicationContext());
                }
            }
        }
        return mInstance;
    }

    private WindowTipManager(Context mContext) {
        this.mContext = mContext;
        initTipView();

        showAnim = new AlphaAnimation(0, 1);
        showAnim.setDuration(500);
        hideAnim = new AlphaAnimation(1, 0);
        hideAnim.setDuration(500);
    }

    /**
     * @Desc 初始化提示视图
     */
    private void initTipView() {
        simpleView = new SimpleView(mContext, R.layout.popup_tip_msg);
        tipViewListener(simpleView);

        /*获取状态栏高度，不让状态栏背景与状态栏重合*/
        int statusBarHeight;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            statusBarHeight = StatusBarUtil.getStatusBarHeight(mContext);
        } else {
            statusBarHeight = 0;
        }
        float i = statusBarHeight / 600;

        FloatWindow
                .with(mContext)
                .setView(simpleView)
                .setTag(WINDOW_TAG)
                .setWidth(Screen.width, 1.0f) //设置悬浮控件宽高
//                .setHeight(Screen.height, 0.2f)
                .setX(0)
                .setY(-statusBarHeight)
                .setGravity(Gravity.TOP | Gravity.START)
                .setDesktopShow(false) //桌面显示
                .setMoveType(MoveType.inactive, 0, 0)
                .setFilter(false
                        , ConfigWifiActivity.class
                        , GuideVideoActivity.class
                        , GuideCompleteActivity.class
                        , SetWifiActivity.class
                        , SetWifiActionActivity.class
                        , SetWifiLinkActivity.class
                        , SetWifiDetailActivity.class)
//                .setMoveStyle(500, new BounceInterpolator())
//                .setViewStateListener(mViewStateListener)
//                .setPermissionListener(mPermissionListener)
//                .setDesktopShow(false)
                .build();
    }

    private void tipViewListener(View simpleView) {
        TextView tvGoto = simpleView.findViewById(R.id.tv_goto);
        BaseUtils.setTextGradient(tvGoto);
        tvGoto.setOnClickListener(v -> {
            Intent intent = new Intent(mContext, SetWifiActivity.class);
            intent.putExtra("titleName", "无线网络");
            mContext.startActivity(intent);
        });
    }

    private boolean needShow() {
        Activity activity = AppManager.getAppManager().currentActivity();
        LogUtils.e("currentActivity = " + activity);
        if (activity != ActivityCollector.getActivity(ConfigWifiActivity.class)
                && activity != ActivityCollector.getActivity(GuideCompleteActivity.class)
                && activity != ActivityCollector.getActivity(SetWifiActivity.class)
                && activity != ActivityCollector.getActivity(SetWifiActionActivity.class)
                && activity != ActivityCollector.getActivity(SetWifiLinkActivity.class)
                && activity != ActivityCollector.getActivity(SetWifiDetailActivity.class)
                && activity != ActivityCollector.getActivity(GuideVideoActivity.class)) {
            return true;
        }
        return false;
    }

    /**
     * @Desc 展示引导提示蒙层
     */
    public void showTip() {
        //引导页完成前不可展示
        DB_Common dbCommon = new DB_Common(mContext);
        String isFirst = dbCommon.get("isFirstOpenApp");
        if (TextUtils.isEmpty(isFirst)) {
            return;
        }
        if (!needShow()) {
            return;
        }
        if (FloatWindow.get(WINDOW_TAG) != null && !FloatWindow.get(WINDOW_TAG).isShowing()) {
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    FloatWindow.get(WINDOW_TAG).show();
                }
            }, 500);
            simpleView.startAnimation(showAnim);
        }
    }

    /**
     * @Desc 关闭引导提示蒙层
     */
    public void dismissTip() {
        if (FloatWindow.get(WINDOW_TAG) != null && FloatWindow.get(WINDOW_TAG).isShowing()) {
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    FloatWindow.get(WINDOW_TAG).hide();
                }
            }, 500);
            simpleView.startAnimation(hideAnim);
        }
    }
}
